package cc.site.main.tools;

import cc.site.main.tools.aliyun.AliyunOssProperties;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.joda.time.DateTime;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

/**
 * @CREATE By yangruomao On 2024/4/13
 */

public class OssTools {

    /**
     * 上传文件
     * 如果文件名带有“ ”， 会替换为“-”
     * @param file
     * @return
     */
    public static String upload(MultipartFile file) {
        String endpoint = AliyunOssProperties.END_POINT;
        String accessKeyId = AliyunOssProperties.KEY_ID;
        String accessKeySecret = AliyunOssProperties.KEY_SECRET;
        String bucketName = AliyunOssProperties.BUCKET_NAME;

        String objectName = UUID.randomUUID().toString().replaceAll("-", "") + file.getOriginalFilename().replaceAll(" ", "-");
        String date = new DateTime().toString("yyyy/MM/dd");
        objectName = date + "/" + objectName;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建PutObject请求。
        ossClient.putObject(bucketName, objectName, inputStream);

        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;
    }


    public static String uploadByPath(String path) {
        // System.out.println("************" + path);
        File file = new File(path);

        String endpoint = AliyunOssProperties.END_POINT;
        String accessKeyId = AliyunOssProperties.KEY_ID;
        String accessKeySecret = AliyunOssProperties.KEY_SECRET;
        String bucketName = AliyunOssProperties.BUCKET_NAME;

        String objectName = UUID.randomUUID().toString().replaceAll("-", "") + file.getName().replaceAll(" ", "-");
        String date = new DateTime().toString("yyyy/MM/dd");
        objectName = date + "/" + objectName;

        //  System.out.println(objectName);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);


        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 创建PutObject请求。
        ossClient.putObject(bucketName, objectName, inputStream);


        String res = "https://" + bucketName + "." + endpoint + "/" + objectName;
        // System.out.println(res);
        return res;
    }

    /**
     * 上传文件，可以自定义文件后缀
     * @param inputStream
     * @param suffix 文件后缀
     * @return
     */
    public static String uploadByFileStream(InputStream inputStream, String suffix) {
        String endpoint = AliyunOssProperties.END_POINT;
        String accessKeyId = AliyunOssProperties.KEY_ID;
        String accessKeySecret = AliyunOssProperties.KEY_SECRET;
        String bucketName = AliyunOssProperties.BUCKET_NAME;

        String objectName = UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix;
        String date = new DateTime().toString("yyyy/MM/dd");
        objectName = date + "/" + objectName;
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 创建PutObject请求。
        ossClient.putObject(bucketName, objectName, inputStream);

        String url = "https://" + bucketName + "." + endpoint + "/" + objectName;
        return url;
    }
    
}
